package timeline
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	[SWF(width="600",height="600",backgroundColor="#bdb8b8",frameRate="30")]
	public class TimeLineSimulation extends Sprite
	{
		private var frameArr:Array;//帧数据
		
		[Embed(source="assets/img.png")]
		private var pic:Class;
		
		private var sbmpd:BitmapData;
		
		private var scene:Bitmap;
		
		private var currentFrame:int = 0;
		
		public function TimeLineSimulation()
		{
			trace(stage.stageWidth+"............"+stage.stageHeight);
			super();
			init();
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			this.addEventListener(Event.ENTER_FRAME,onFrame);
		}
		
		public function init():void {
			sbmpd = (new pic() as Bitmap).bitmapData;
//			trace(stage.stageWidth+"............"+stage.stageHeight);
			frameArr = new Array();
			scene = new Bitmap(new BitmapData(sbmpd.width/8,sbmpd.height/8));
			scene.x = stage.stageWidth/2 - scene.width/2;
			scene.y = stage.stageHeight/2 - scene.height/2;
			var runbmpd:BitmapData =  new BitmapData(sbmpd.width/8,sbmpd.height/8);
			runbmpd.copyPixels(sbmpd,new Rectangle(sbmpd.width/8,0,sbmpd.width/8,sbmpd.height/8),new Point(0,0));
			scene.bitmapData = runbmpd;
			this.addChild(scene);
			run  ();
		}
		
		public function run():void {
			animate(1,1);
		}
		
		public function jump():void {
			animate(2,6);
		}
		
		public function whirl():void {
			animate(4,0);
		}
		
		public function bomb():void {
			animate(5,9);
		}
		
		public function animate(row:int,blank:int):void {
			frameArr = new Array();
			currentFrame = 0;
			for(var i:int=0;i<8;i++) {
				var runbmpd:BitmapData =  new BitmapData(sbmpd.width/8,sbmpd.height/8);
				runbmpd.copyPixels(sbmpd,new Rectangle(i*sbmpd.width/8,(row-1)*sbmpd.height/8,sbmpd.width/8,sbmpd.height/8),new Point(0,0));
				frameArr.push(runbmpd);
				for(var j:int=0;j<blank;j++) {
					frameArr.push(0);
				}
			}
		}
		
		public function onFrame(event:Event):void {
			var btmp:BitmapData = frameArr[currentFrame] as BitmapData;
			if(btmp) scene.bitmapData = btmp;
			if(currentFrame==frameArr.length-1) {
				currentFrame = 0;
			}else {
				currentFrame++;
			}
		}
	}
}